package com.lj.ljminsu.controller;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.lj.ljminsu.bean.JsonResultVo;
import com.lj.ljminsu.bean.TjUser;
import com.lj.ljminsu.service.IUserDaoService;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * 注册
 */
@RestController
@RequestMapping("user")
public class RegisterController {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private IUserDaoService iUserDaoService;

    /**
     * 注册
     *
     * @param userPhone
     * @param password
     * @param email
     * @param verifycode
     * @return
     */
    @CrossOrigin
    @RequestMapping("register")
    public JsonResultVo register(String userPhone, String password, String email, String verifycode,String verifyCodePic) {
        TjUser tjUser = new TjUser();
        JsonResultVo jsonResultVo = new JsonResultVo();
        String test = stringRedisTemplate.opsForValue().get( "text");
        String phoneSecurityCode = stringRedisTemplate.opsForValue().get( userPhone+"phoneSecurityCode");
        System.out.println(phoneSecurityCode);
        String existPhone = iUserDaoService.checkPhone(userPhone);
        System.out.println(existPhone);
        String existEmail = iUserDaoService.checkUserByEmail(email);
        if (!verifyCodePic.equalsIgnoreCase(test)) {
            jsonResultVo.setCode(4);
            jsonResultVo.setMsg("图形验证码错误");
            return jsonResultVo;
        }
        if (existPhone == null || "".equals(existPhone)) {
            if (existEmail == null || "".equals(existEmail)) {
                if (verifycode.equals(phoneSecurityCode)) {
                    tjUser.setEmail(email);
                    tjUser.setPassword(password);
                    tjUser.setUserPhone(userPhone);
                    tjUser.setSalt("123456");
                    Date date = new Date();
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                    String time = simpleDateFormat.format(date);
                    tjUser.setCreateTime(time);
                    iUserDaoService.addUser(tjUser);
                    jsonResultVo.setCode(0);
                    jsonResultVo.setMsg("注册成功");
                    return jsonResultVo;
                }
                jsonResultVo.setCode(3);
                jsonResultVo.setMsg("您的手机验证码输入有误，请重新输入");
                return jsonResultVo;
            }
            jsonResultVo.setCode(2);
            jsonResultVo.setMsg("该邮箱已经绑定其他账户");
            return jsonResultVo;
        }
        jsonResultVo.setCode(1);
        jsonResultVo.setMsg("您的手机号已注册过,请登录");
        return jsonResultVo;

    }


}
